﻿#------------------------------------------------------------------
#add_function.cmake
#please dont modify this cmake
#2018-06-17 01:59:32
#------------------------------------------------------------------
#命令名列表
IF(WIN32)
    SET(SCRIPT_SUFFIX bat)
    SET(CMD_SEP &)
    SET(MOVE "move /y")
    SET(COPY "copy /y")
    SET(COPY_DIR "xcopy /s /q /y /i /r /h")
    SET(MK_DIR "mkdir")
    SET(RM "del /q")
    SET(CD "cd /d")
    SET(RM_DIR "rd /s /q")
    #SET(PATH_SEP "\\")
ELSEIF(APPLE)
    SET(SCRIPT_SUFFIX sh)
    SET(CMD_SEP &&)
    SET(MOVE "mv")
    SET(COPY "cp -f")
    SET(COPY_DIR "cp -R -L -f -apc")
    SET(MK_DIR "mkdir -p")
    SET(RM "rm -f")
    SET(CD "cd")
    SET(LN "ln -sfn")
    SET(RM_DIR "rm -rf")
    #SET(PATH_SEP "/")
ELSE()
    SET(SCRIPT_SUFFIX sh)
    SET(CMD_SEP &&)
    SET(MOVE "mv")
    SET(COPY "cp -f")
    SET(COPY_DIR "cp -rf")
    SET(MK_DIR "mkdir -p")
    SET(RM "rm -f")
    SET(CD "cd")
    SET(LN "ln -sf")
    SET(RM_DIR "rm -rf")
    #SET(PATH_SEP "/")
ENDIF()


#---------------------------------------------------------------------------------
#内部函数
#---------------------------------------------------------------------------------
#废弃
FUNCTION(GET_MKDIR FILEPATH COMMAND)
    SET(${COMMAND} ${MK_DIR} PARENT_SCOPE)
    #message(${COMMAND})
ENDFUNCTION()

#获取错误码的命令
FUNCTION(GET_ERRCODE COMMAND)
    IF(WIN32)
        SET(${COMMAND} "${CMAKE_CURRENT_LIST_DIR}/win_system_errorcode.bat" PARENT_SCOPE)
    ELSE()
        SET(LINUX_COMMAND "${CMAKE_CURRENT_LIST_DIR}/linux_system_errorcode.sh")
        EXECUTE_PROCESS(COMMAND chmod "+x ${LINUX_COMMAND}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
        SET(${COMMAND} "${LINUX_COMMAND}" PARENT_SCOPE)
    ENDIF()
    #message(${COMMAND})
ENDFUNCTION()

#文件清空的命令
FUNCTION(GET_EMPTY_FILE COMMAND)
    IF(WIN32)
        SET(${COMMAND} "${CMAKE_CURRENT_LIST_DIR}/win_empty_file.bat" PARENT_SCOPE)
    ELSE()
        SET(LINUX_COMMAND "${CMAKE_CURRENT_LIST_DIR}/linux_empty_file.sh")
        EXECUTE_PROCESS(COMMAND chmod "+x ${LINUX_COMMAND}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
        SET(${COMMAND} "${LINUX_COMMAND}" PARENT_SCOPE)
    ENDIF()
    #message(${COMMAND})
ENDFUNCTION()

#废弃
FUNCTION(GET_WRITE_FILE COMMAND)
    IF(WIN32)
        SET(${COMMAND} "${CMAKE_CURRENT_LIST_DIR}/win_write_file.bat" PARENT_SCOPE)
    ELSE()
        SET(LINUX_COMMAND "${CMAKE_CURRENT_LIST_DIR}/linux_write_file.sh")
        EXECUTE_PROCESS(COMMAND chmod "+x ${LINUX_COMMAND}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
        SET(${COMMAND} "${LINUX_COMMAND}" PARENT_SCOPE)
    ENDIF()
    #message(${COMMAND})
ENDFUNCTION()

FUNCTION(GET_COPY_DIR_AND_FILE COMMAND)
    IF(WIN32)
        SET(${COMMAND} "${CMAKE_CURRENT_LIST_DIR}/win_cp_files.bat" PARENT_SCOPE)
    ELSE()
        SET(LINUX_COMMAND "${CMAKE_CURRENT_LIST_DIR}/linux_cp_files.sh")
        EXECUTE_PROCESS(COMMAND chmod "+x ${LINUX_COMMAND}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
        SET(${COMMAND} "${LINUX_COMMAND}" PARENT_SCOPE)
    ENDIF()
    #message(${COMMAND})
ENDFUNCTION()

#---------------------------------------------------------------------------------
#外部函数
#---------------------------------------------------------------------------------
FUNCTION(SYSTEM_ERRCODE CMD_EXEC ERRCODE)
    GET_ERRCODE(COMMAND)
    #message(${COMMAND})
    EXECUTE_PROCESS(COMMAND ${COMMAND} "${CMD_EXEC}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
    #MESSAGE(result:${RESULT}\ output:${OUTPUT})
    SET(${ERRCODE} ${OUTPUT} PARENT_SCOPE)
ENDFUNCTION()

FUNCTION(SYSTEM CMD_EXEC)
    SYSTEM_ERRCODE(${CMD_EXEC} ERRCODE)
ENDFUNCTION()

FUNCTION(MKDIR DIRECTORY)
    FILE(MAKE_DIRECTORY ${DIRECTORY})
ENDFUNCTION()

FUNCTION(EMPTY_FILE FILE)
    GET_EMPTY_FILE(COMMAND)
    EXECUTE_PROCESS(COMMAND ${COMMAND} "${FILE}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
ENDFUNCTION()

FUNCTION(WRITE_LINE FILE CONTENT)
    FILE(APPEND ${FILE} ${CONTENT})
ENDFUNCTION()

FUNCTION(COPY_DIR_AND_FILE SOURCE PATTERN TARGET)
    GET_COPY_DIR_AND_FILE(COMMAND)
    EXECUTE_PROCESS(COMMAND ${COMMAND} "${SOURCE}" "${PATTERN}" "${TARGET}" RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUTPUT)
ENDFUNCTION()

#输出用户主目录路径
FUNCTION(USER_HOME PATH)
    IF(WIN32)
        SET(${PATH} "$ENV{HOMEDRIVE}$ENV{HOMEPATH}" PARENT_SCOPE)
    ELSE()
        SET(${PATH} "$ENV{HOME}" PARENT_SCOPE)
    ENDIF()
ENDFUNCTION()

FUNCTION(USER_CONFIG_PATH PATH)
    IF(WIN32)
        SET(${PATH} "$ENV{APPDATA}" PARENT_SCOPE)
    ELSE()
        SET(${PATH} "$ENV{HOME}" PARENT_SCOPE)
    ENDIF()
ENDFUNCTION()

#???
FUNCTION(SET_INTERNAL VARNAME)
    SET(${VARNAME} ${ARGV1} CACHE INTERNAL "${VARNAME}")
ENDFUNCTION()
